package nb.photoviewer.ImageEditor;

import javafx.fxml.FXML;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseEvent;

import java.io.File;

public class EditorManager {
    private File editImage;;
    @FXML
    private ImageView imageView;

    //放大缩小
    private double orgSceneX, orgSceneY;
    private double orgTranslateX, orgTranslateY;


    //构造方法，传数据用
    public EditorManager(ImageView imageView){
        this.imageView=imageView;
        System.out.println("imageview"+imageView);
        System.out.println("EditorManager  构造方法   中");
    }
    public  void SetImage(File editImage){
        this.editImage=editImage;
        System.out.println("editorManager"+editImage.getName());//测试一下有没有传过来；
        this.SetImageView();
    }
    public void SetImageView(){
        // 加载图片文件
        Image image = new Image(this.editImage.toURI().toString());
        // 设置图片到ImageView中
        imageView.setImage(image);
        // 初始化位置
        imageView.setPreserveRatio(true);
        // 将图片平移位置重置为0，在切换图片的时候起作用
        imageView.setTranslateX(0);
        imageView.setTranslateY(0);

        //让图片旋转角度为0
        imageView.setRotate(0);
        // 重置缩放比例,切换图片时，上一张的放大缩小已经不起作用了
        imageView.setScaleX(1);
        imageView.setScaleY(1);

        // 设置事件处理器————拖动
        imageView.setOnMousePressed((MouseEvent event)->{
            orgSceneX = event.getSceneX();
            orgSceneY = event.getSceneY();
            orgTranslateX = imageView.getTranslateX();
            orgTranslateY = imageView.getTranslateY();
        });
        imageView.setOnMouseDragged((MouseEvent event)->{
            double offsetX = event.getSceneX() - orgSceneX;
            double offsetY = event.getSceneY() - orgSceneY;
            double newTranslateX = orgTranslateX + offsetX;
            double newTranslateY = orgTranslateY + offsetY;
            imageView.setTranslateX(newTranslateX);
            imageView.setTranslateY(newTranslateY);
        });
    }

}
